package edu.itstudy.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.itstudy.bean.User;
import edu.itstudy.services.IUserServices;
import edu.itstudy.services.impl.IUserServicesImpl;

public class RegisterServlet extends HttpServlet {
	
	private IUserServices iuserServices = new IUserServicesImpl();
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获取用户注册信息
		 * 把信息交给services帮忙注册
		 * services把注册结果告诉servlet
		 * services要DAO帮忙注册，DAO告诉services插入是否成功
		 * DAO通过JDBC连接数据库操作
		 * 根据services注册结果响应用户		
		 * true 回首页
		 * false 注册失败
		 */
		//设置字符编码
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String age = request.getParameter("age");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String picture = "images/22.jpg";
		String intro = "";
		int recommend = 0;
		int flag = 0;
		
		User user = new User(0,name,password,Integer.parseInt(age),sex,phone,address,picture,intro,recommend,flag);
		boolean result = iuserServices.register(user);
		
		//业务流程控制
		if(result) {
			response.sendRedirect("index");
		}else {
			request.setAttribute("info","用户注册失败！");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
	}
}
